package com.wfit.service.impl;


import com.wfit.context.UserContext;
import com.wfit.file.service.FileStorageService;
import com.wfit.result.Result;
import com.wfit.service.UploadService;
import com.wfit.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.wfit.po.Users;


import java.io.IOException;
import java.io.InputStream;

@Service
public class UploadServiceImpl implements UploadService {
    @Autowired
    private FileStorageService fileStorageService;
    @Autowired
    private UsersService usersService;

    @Override
    public String upload(MultipartFile image) throws IOException {
        String originalFilename = image.getOriginalFilename();
        InputStream inputStream = image.getInputStream();
        String url = fileStorageService.uploadImgFile("", originalFilename, inputStream);

        return url;
    }

    @Override
    public void uploadAvatar(MultipartFile image) throws IOException {
        String url = upload(image);
        usersService.lambdaUpdate().eq(Users::getId, UserContext.getUser()).set(url != null,Users::getAvatar, url).update();
    }
}
